<?php

namespace App\Repository\Shop;

use App\Models\Shop\DistGoodsSetModel as model;
use Illuminate\Support\Facades\Cache;
use App\Repository\RoleRepository;
use Illuminate\Support\Facades\DB;
use App\Repository\Admin\AdminUsersRepository as user;
use App\Repository\BaseRepository;
use App\Repository\Product\GoodsRepository;
use App\Repository\Shop\SuppilerRepository as supp;
use App\Http\Controllers\Admin\SuppilerController;

class DistGoodsSetRepository extends  BaseRepository
{
  private $model;
  private $adminuser;
  private $disModel;
  public function __construct()
  {
    $this->model = new model();
    $this->adminuser = new user();
    $this->disModel = new supp();
    parent::__construct();
  }
  //列表
  public function index($where)
  {
    $cron = $where;
    $cron['order'] = ['orderby' => 'id', 'orderdir' => 'desc'];
    // $cron['select'] = ['name', 'phone', 'created_at', 'updated_at', 'shop_id', 'id'];
    $data = $this->search($this->model, $cron);
    // if (!empty($data['data'])) {
    //   $data['data'] = usfirstArr($data['data']);
    // }
    return $data;
  }
  //添加
  public function add($data)
  {
    //必填 字段
    $mustField = [
      'goods_id',
    ];
    $optionalFields = $this->model->getTableField();
    $msgField = [
      'goods_id' => '商品不能为空'
    ];
    $insertData  = $this->getRealData($data, $mustField, $optionalFields, $msgField);
    if (!empty($insertData) && is_string($insertData)) {
      //返回错误参数
      return $this->returnMsg(
        2,
        $insertData
      );
    }
    // 查询下商品信息
    // 查询供应商的代码
    $where = [
      'shop_id' => $insertData['shop_id'],
      'goods_id' => $insertData['goods_id']
    ];
    $isTrue = $this->model->where($where)->first();
    if ($isTrue) {
      return $this->returnMsg(10, '您已设置该商品,请前去修改');
    }
    if (isset($insertData['dist_type'])) {
      $insertData['dist_type_name'] = $this->model->getTypeName($insertData['dist_type']);
    }
    $insertData['created_at'] = date('Y-m-d H:i:s');
    // $insertData['shop_id'] = $data->user->id;
    // $insertData['shop_str'] = $data->user->shop_str;
    $res = $this->model->insert($insertData);

    if (!empty($insertData['dist_goods']) && $insertData['dist_goods'] == 1) {
      if (!empty($insertData['dist_type']) && $insertData['dist_type'] >= 1) {
        return $this->allBatchGoods($insertData['shop_id'], $insertData['goods_id'], $insertData['dist_shop_id'], $insertData['dist_type'], !empty($data) ? $data : []);
      }
    }
    // var_dump($insertData, 1111111111);
    // die();
    if ($res) {
      return $this->returnMsg(200, '添加成功');
    }
    return $this->returnMsg(1, '添加失败');
  }
  // 批量添加分销商商品
  public function allBatchGoods($shopId, $goodsId, $shopIdArr, $type, $dataRequest = [])
  {
    $where = [
      'supplier_id' => $shopId,
    ];
    $field = ['shop_id as id', 'name', 'supplier_name', 'supplier_id'];
    // 查询出来的所有分销商
    if (!empty($shopIdArr)) {
      $shopIdArr = explode(',', $shopIdArr);
    }
    // var_dump($shopIdArr);
    $data = $this->disModel->getAllData($where, $field);
    // var_dump($data, 222222222); // 所有代理
    // var_dump($goodsId, 3333333333); // 商品id
    // var_dump($shopIdArr, 44444444444); // 店铺id
    switch ($type) {
      case 1:
        // 所有都创建
        break;
      case 2:
        // 部分可创建
        if (empty($shopIdArr)) {
          $data = [];
        }
        // $data = $shopIdArr;
        if (!empty($data) && !empty($shopIdArr)) {
          foreach ($data as $key => $v) {
            // $isTrue = in_array($v->id, $shopIdArr);
            if (!in_array($v['id'], $shopIdArr)) {
              unset($data[$key]);
            }
          }
        }
        break;
      case 3:
        // 部分不可建
        if (!empty($data) && !empty($shopIdArr)) {
          foreach ($data as $key => $v) {
            // $isTrue = in_array($v->id, $shopIdArr);
            if (in_array($v['id'], $shopIdArr)) {
              unset($data[$key]);
            }
          }
        }
        break;
    }
    // 批量创建商品
    $isPlAll = false;
    if (!empty($data)) {

      $modelGoods = new GoodsRepository();

      foreach ($data as $key => $v) {
        if ($v['id'] == $v['supplier_id']) {
          continue;
        }
        // 循环添加商品 
        $dataUser['user'] = [
          'shop_id' => $v['id'],
          'shop_name' => $v['name'],
          'id' => !empty($dataRequest['user']['id']) ? $dataRequest['user']['id'] : '',
          'name' => !empty($dataRequest['user']['name']) ? $dataRequest['user']['name'] : '',
        ];
        // var_dump($dataRequest, 11111111);
        $insertDatas = [
          'shop_name' => $v['name'],
          'supplier_name' => $v['supplier_name'],
          'shop_id' => $v['id'],
          'shopId' => $v['supplier_id'],
          'goods_id' => $goodsId
        ];
        $insertData = array_merge($insertDatas, $dataUser);
        $add = $modelGoods->addSuppilerGoods($insertData);
        // var_dump($add);
        if ($add) {
          $isPlAll = true;
        }
      }
    }
    if ($isPlAll) {
      return $this->returnMsg(200, '添加成功');
    }
    return $this->returnMsg(101, '有部分添加失败,请检查');
    // var_dump($data, 22222222);
  }
  //删除
  public function del($where)
  {
    $flag = true;
    $msg = '未知错误';
    $code = 1;
    if (empty($where)) {
      $flag = false;
      $msg = '删除条件不能为空';
      $code = 2;
    }
    if ($flag) {
      $res = $this->model->where($where)->get();
      if (!$res) {
        $flag = false;
        $msg = '未查询到数据';
        $code = 3;
      }
    }
    if ($flag) {
      $del = $this->model->where($where)->delete();
      if ($del) {
        $msg = '删除成功';
        $code = 200;
      }
    }
    return $this->returnMsg($code, $msg);
  }
  //修改
  public function edit($where, $data, $isMust = false)
  {
    if (empty($where)) {
      return $this->returnMsg(1, '条件不能为空');
    }
    if (empty($data)) {
      return $this->returnMsg(2, '修改参数不能为空');
    }
    //过滤字段
    //过滤字段
    $optionalFields = $this->model->getTableField();
    $field  = $this->getRealData($data, [], $optionalFields, []);
    if (isset($field['dist_type'])) {
      $field['dist_type_name'] = $this->model->getTypeName($field['dist_type']);
    }
    //先查询是否存在
    if (!empty($field['created_at'])) {
      unset($field['created_at']);
    }
    $res = $this->model->where($where)->update($field);
    if ($isMust && $res) {
      return $this->returnMsg(200, '修改成功');
    }
    if (!$isMust) {
      return $this->returnMsg(200, '修改成功');
    }
    return $this->returnMsg(3, '请重新修改');
  }
  //列表查询
  public function lists($where = [], $page = 1, $limit = 10)
  {
    $res = $this->search($this->model, $where, $page, $limit);

    return $res;
  }
  public function getOne($where)
  {
    return $this->model->where($where)->first();
  }
  public function getAllData($where, $field = ['*'])
  {
    return $this->model->where($where)->select($field)->get()->toArray();
  }
}
